<template>
  <div>
    <el-form :inline="true" ref="searchMap" :model="searchMap" class="demo-form-inline">
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item prop="supplierId">
        <el-input v-model="searchMap.supplierID" placeholder="商品编号"></el-input>
      </el-form-item>
      <el-form-item prop="supplierName">
        <el-input
          v-model="searchMap.supplierName"
          @click.native="dialogTableVisible=true"
          readonly
          placeholder="选择供应商"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="add">新增</el-button>
        <el-button @click="resetForm('searchMap')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" border style="width: 100%" height="470px">
      <el-table-column label="序号" align="center" type="index"></el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="supplierID" label="商品编码"></el-table-column>
      <el-table-column prop="spec" label="规格"></el-table-column>
      <el-table-column prop="retailPrice" label="零售价"></el-table-column>
      <el-table-column prop="purchasePrice" label="进货价"></el-table-column>
      <el-table-column prop="storageNum" label="库存数量"></el-table-column>
      <el-table-column prop="supplierName" label="供应商"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <el-dialog title="选择供应商" :visible.sync="dialogTableVisible" width="500px">
      <supplier :isgood="true" @select="select"></supplier>
    </el-dialog>
    <el-dialog title="编辑商品" :visible.sync="dialogFormVisible" width="500px">
      <el-form :rules="rules" :model="addForm" style="width:450px" ref="addForm">
        <el-form-item label="商品名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="addForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品编码" :label-width="formLabelWidth" prop="supplierID">
          <el-input v-model="addForm.supplierID" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品规格" :label-width="formLabelWidth" prop="spec">
          <el-input v-model="addForm.spec" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="零售价" :label-width="formLabelWidth" prop="retailPrice">
          <el-input v-model="addForm.retailPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="进货价" :label-width="formLabelWidth" prop="purchasePrice">
          <el-input v-model="addForm.purchasePrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" :label-width="formLabelWidth" prop="storageNum">
          <el-input v-model="addForm.storageNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="供应商" :label-width="formLabelWidth" prop="supplierName">
          <el-input
            v-model="addForm.supplierName"
            autocomplete="off"
            readonly
            placeholder="选择供应商"
            @click.native="addOption"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addForm.id===null?addDate('addForm'):editDate('addForm')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import goodApi from "@/api/goods";
import Supplier from "../supplier";
export default {
  data() {
    return {
      page: 1,
      size: 10,
      total: 0,
      searchMap: {
        name: "",
        supplierID: "",
        supplierName: ""
      },
      list: [],
      dialogFormVisible: false,
      dialogTableVisible: false,
      formLabelWidth: "90px",
      addForm: {
        id: null,
        name: "",
        supplierId: "",
        spec: "",
        retailPrice: "",
        purchasePrice: "",
        storageNum: "",
        supplierName: ""
      },
      isadd: false,
      rules: {
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        supplierID: [
          { required: true, message: "商品编码不能为空", trigger: "blur" }
        ],
        retailPrice: [
          { required: true, message: "商品编码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.search();
  },
  components: { Supplier },

  methods: {
    search(page, size, searchMap) {
      goodApi.search(this.page, this.size, this.searchMap).then(res => {
        const resp = res.data;
        if (resp.flag) {
          this.list = resp.data.rows;
          this.total = resp.data.total;
        } else {
          // 获取失败
        }
      });
    },
    handleSizeChange(val) {
      this.size = val;
      this.search();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.search();
    },
    add() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["addForm"].resetFields();
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    select(val) {
      if (this.isadd) {
        this.addForm.supplierName = val.name;
      } else {
        this.searchMap.supplierName = val.name;
      }
      this.dialogTableVisible = false;
    },
    addOption() {
      this.dialogTableVisible = true;
      this.isadd = true;
    },
    addDate(formName) {
      this.$refs[formName].validate(validate => {
        if (validate) {
          goodApi.addForm(this.addForm).then(res => {
            const resp = res.data;
            if (resp.flag) {
              this.$message({
                message: resp.message,
                type: "success"
              });
              this.dialogFormVisible = false;
              this.search();
            } else {
              //新增失败
            }
          });
        }
      });
    },
    handleEdit(id) {
      this.add();
      goodApi.getById(id).then(res => {
        const resp = res.data;
        if (resp.flag) {
          this.addForm = resp.data;
        }
      });
    },
    editDate(formName) {
      this.$refs[formName].validate(validate => {
        if (validate) {
          goodApi.edit(this.addForm).then(res => {
            const resp = res.data;
            if (resp.flag) {
              this.$message({
                message: "编辑商品成功",
                type: "success"
              });
              this.dialogFormVisible = false;
              this.search();
            }
          });
        }
      });
    },
    handleDelete(id) {
      this.$confirm("确认删除这条记录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(() => {
          goodApi.delete(id).then(res => {
            const resp = res.data;
            if (resp.flag) {
              this.$message({
                message: "删除成功",
                type: "success"
              });
            } else {
              this.$message({
                message: "删除失败",
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped>
</style>